package com.awspaas.user.apps.cpq.controller;

import java.util.HashMap;
import java.util.Map;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.mvc.view.ResponseObject;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.sdk.local.SDK;
import com.actionsoft.sdk.local.api.BOCopyAPI;
import com.actionsoft.sdk.local.api.BOQueryAPI;

public class CPQ_GetNewModelWeb {

	public String getNewModel(UserContext userContext, String bindId,String type) {
		try {
			BOQueryAPI dataQuery=SDK.getBOAPI().query("BO_EU_CPQ_MODULE_MSG").bindId(bindId);
			BOQueryAPI gridsQuery=SDK.getBOAPI().query("BO_EU_CPQ_MODULE_MSG_GRID").bindId(bindId);
			BOQueryAPI parameterQuery=SDK.getBOAPI().query("BO_EU_CPQ_MODULE_MSG_PRICE_PARAMETERS").bindId(bindId);
			//创建新的模块
			String pid=SDK.getProcessAPI().createBOProcessInstance("obj_bc469796b31b4eff9d96cdd5e020d447",userContext.getUID(), "").getId();
			BOCopyAPI newData=dataQuery.copyTo("BO_EU_CPQ_MODULE_MSG", pid);
			newData.removeMapping("");
			if(type.equals("创建新模块")) {
				newData.addNewData("MKBM","");
				newData.addNewData("GXSJ","");
				newData.addNewData("BBH","V1.0");
				newData.addNewData("VERSION","1");
			}else if(type.equals("创建新版本")) {
				BO bo=dataQuery.detail();
				String vString=bo.getString("VERSION");
				int num=Integer.valueOf(vString);
				int newNum=num+1;
				newData.addNewData("GXSJ","");
				newData.addNewData("BBH","V"+newNum+".0");
				newData.addNewData("VERSION",newNum);
			}
			
			
			newData.addNewData("STATUS","拟制");
			newData.addNewData("CJLX",type);
			newData.exec();
			BOCopyAPI newGrids=gridsQuery.copyTo("BO_EU_CPQ_MODULE_MSG_GRID", pid);
			newGrids.exec();
			BOCopyAPI newParameter=parameterQuery.copyTo("BO_EU_CPQ_MODULE_MSG_PRICE_PARAMETERS", pid);
			newParameter.exec();
			//SDK.getBOAPI().query("").bindId(bindId);
			ResponseObject ro = ResponseObject.newOkResponse();
			Map<String, String> map=new HashMap<String, String>();
			map.put("bindId", pid);
			ro.setData(map);
			return ro.toString();
		} catch (Exception e) {
			throw e;
		}
		
	}

}
